#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <elf.h>
#include <fcntl.h>
#include <android_runtime/AndroidRuntime.h>
#include "common.h"
#include "JavaHook/JavaMethodHook.h"


#define LOG_TAG "DEBUG"
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, fmt, ##args)

#ifdef __cplusplus
extern "C" {
#endif
int hook_entry(char * a);
#ifdef __cplusplus
}
#endif



static JNIEnv* jni_env;

int hook_entry(char * a)
{
    LOGI("hook_entry is Executing!!");

    JavaVM* jvm = android::AndroidRuntime::getJavaVM();

    LOGI("jvm is %p",jvm);

    jvm->AttachCurrentThread(&jni_env, NULL);

    LOGI("jni is %p",jni_env);
    HookInfo *info = (HookInfo *)malloc(sizeof(HookInfo));
//public Cursor query(Uri url, String[] projectionIn, String selection,String[] selectionArgs, String sort)
    int isstatic = 0;

    info->classDesc = "com/android/providers/telephony/SmsProvider";
    info->methodName = "query";
    info->methodSig = "(Landroid.net.Uri;[Ljava.lang.String;Ljava.lang.String;[Ljava.lang.String;Ljava.lang.String;)Landroid.database.Cursor;";

    info->isStaticMethod = isstatic == JNI_TRUE;

    java_method_hook(jni_env, info);

    LOGI("main is finished!!");

    jvm->DetachCurrentThread();

    return 0;
}


#if 0
//void breforemain() __attribute__((constructor));
__attribute__((constructor))
void breforemain()
{
    LOGD("my auto exec test\n");
}


//////////////////////////////////
void Main()
{
    LOGD("enter Main\n");
}

static void* _main(void*){
    Main();
    return NULL;
}

class EntryClass {
public:

    EntryClass() {
        pthread_t tid;
        pthread_create(&tid, NULL, _main, NULL);
        pthread_detach(tid);
    }

} boy;

#endif
